const util = require('../../../utils/checkidcard.js');
var api = require('../../../config/api.js');//接口api地址
const app = getApp();

Page({
  data: {
    showbasicinformation: false,//收起展开
    columns: ["请选择性别", "未知", "男", "女"],
    gender: 0 || wx.getStorageSync("gender") * 1,
    idcardcolumns: ["请选择证件类型", "身份证", "港澳居民身份证"],
    idcard: 0 || wx.getStorageSync("idcard") * 1,
    username: "",
    phone: "",
    idnumber: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 2000
    })
  },
  //收起
  packupbasicinformation: function () {
    this.setData({
      showbasicinformation: !this.data.showbasicinformation
    })
  },
  //展开
  showbasicinformation: function () {
    this.setData({
      showbasicinformation: !this.data.showbasicinformation
    })
    console.log(this.data.show)
  },
  //名称
  inputusername: function (e) {
    this.setData({
      username: e.detail.value
    });
  },
  //手机
  inputphone: function (e) {
    this.setData({
      phone: e.detail.value
    });
  },
  //证件号码
  inputidcard: function (e) {
    this.setData({
      idnumber: e.detail.value
    });
  },
  //性别选择
  pickSex: function (e) {
    this.setData({
      gender: e.detail.value
    });
    // console.log("当前选择性别-sex", e.detail.value);
  },
  //身份证选择
  pickIdCard: function (e) {
    this.setData({
      idcard: e.detail.value
    });
  },

  /**
   * 聚焦函数
   */
  inputFocus: function (t) {
    // this.setData({
    //   [t.target.dataset.id]: t.detail.value
    // });
  },

  /**
   * 失焦函数
   */
  inputBlur: function (t) {
    // this.setData({
    //   [t.target.dataset.id]: t.detail.value.length ? t.detail.value : ""
    // });
  },
  //表单提交
  bind:function(e){
    console.log(e)
    var that = this;
    var value = e.detail.value
    var name = value.username;
    var phone = value.phone;
    var idnumber = value.idnumber;
    var sex = that.data.columns[that.data.gender]; //性别
    // var personals = app.globalData.personals;
    var id=wx.getStorageSync("usermsg").id;
    console.log("newid" + id)
    if (name == "") {
      wx.showToast({
        title: '请输入姓名！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (phone == "") {
      wx.showToast({
        title: '请输入手机号码！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (idnumber == "") {
      wx.showToast({
        title: '请输入证件号码！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (sex == 0) {
      wx.showToast({
        title: '请选择性别！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (!util.checkIdCard(this.data.idnumber)) { //验证身份证号
      wx.showToast({
        title: '请输入正确的身份证号！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    }

    //验证手机号码
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号格式有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: api.NewPatients,
      data: {
        creatorid: id,
        name: name,
        tel: phone,
        sex: that.data.gender,
        type: that.data.idcard,//证件类型
        idcard:that.data.idnumber,
       
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        wx.hideLoading()
        if (res.data.code == 0) {
          wx.showModal({
            title: '新增成功！',
            content: '继续新增患者！',
            success: function (res) {
              if (res.confirm) {
                that.setData({
                  username:"",
                  phone:"",
                  idnumber:"",
                  idcard:0,
                  gender:0
                })
              } else if (res.cancel) {
                wx.switchTab({
                  url: '../index/index',
                })
              }
            }
          })
        } else {
          wx.showModal({
            title: '新增失败，请检查相应信息！',
            content: res.data.msg,
            showCancel: false
          });
        }
      }
    });
  }
 
})